<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function p($data){
    echo "<pre>";
    var_dump($data);
    echo "</pre>";
}

/**
 *  公共输出方法
 * @param  int      $errcode     错误状态码，0表示成功
 * @param  arraay   $data       反馈数据
 * @return array
*/
function output($errcode = 0 , $data = []){
    $response = [
        "errcode" => $errcode,
        "msg"     => errmsg($errcode),
    ];
    if($data){
        $response["data"] = $data;
    }
    header("Content-type:application/json;charset=utf-8");
    echo json_encode($response);die;
}

/**
 *  错误输出
 * @param  int $errcode 错误编码
 * @return  array
*/
function errmsg($errcode){
    $langcode = 0 ;
    if(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){
        $lang=substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,4);
        if(preg_match("/zh-c/i",$lang)){
            $langcode = 0 ;
        }else{
            $langcode = 1 ;
        }
    }

    $errmsg = lang($langcode);

    return isset($errmsg[$errcode]) ? $errmsg[$errcode] : $errmsg[-1];
}

function lang($lang = 0){
    $arr = [
        [
            -1      => '未知错误',
            0       => 'SUCCESS',
            1001    => '缺少appid',
            1002    => '签名失败',
            1003    => 'app不存在',
            1004    => '缺失必要参数',
            1005    => '操作失败',
            2000    =>  '输入数据格式错误',
            2001    => '内容不存在',
            2002    => '获取服务器数据失败',
        ]
    ];
    return $arr[$lang];
}


/**
 *  随机生成字符串
*/
/**
 * 随机字符
 * @param number $length 长度
 * @param string $type 类型
 * @param number $convert 转换大小写
 * @return string
 */
function randstr($length=6, $type='string', $convert=0){
    $config = array(
        'number'=>'1234567890',
        'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
        'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    );

    if(!isset($config[$type])) $type = 'string';
    $string = $config[$type];

    $code = '';
    $strlen = strlen($string) -1;
    for($i = 0; $i < $length; $i++){
        $code .= $string{mt_rand(0, $strlen)};
    }
    if(!empty($convert)){
        $code = ($convert > 0)? strtoupper($code) : strtolower($code);
    }
    return $code;
}

/**
 * 生成唯一订单
 */
function createOrderId(){
    return 'CT' . date('YmdHis').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}

function password($char , $code){
    return md5( md5($char) . $code );
}

/**
 * 远程请求数据
 *  @param string $url 远程请求地址
 *  @param string $array 请求提交的数据
 *  @return string 远程返馈的结果
 */
function https_request($url, $data = null , $headers = null) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    if(!empty($headers)){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    //设置curl默认访问为IPv4
    if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    }
    $output = curl_exec($curl);
    curl_close($curl);
    return ($output);
}